//
//  LCModifyPassViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/11.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCModifyPassViewController.h"
#import "ModifyPasswordModel.h"
#import "CountHeight.h"

@interface LCModifyPassViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPassword;//旧密码
@property (weak, nonatomic) IBOutlet UITextField *password;//新密码
@property (weak, nonatomic) IBOutlet UITextField *surePass;//确定密码
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (strong,nonatomic)ModifyPasswordModel  *modifyModel;
@property (strong,nonatomic)NSMutableDictionary  *dataDic;
@end

@implementation LCModifyPassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"修改密码"];
    _modifyModel = [[ModifyPasswordModel alloc]init];
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [self loadCustomeView];
}
- (void)loadCustomeView
{
    _commitBtn.layer.cornerRadius = 3;
    _commitBtn.layer.masksToBounds = YES;
    [_oldPassword addTarget:self action:@selector(changePhone:) forControlEvents:UIControlEventEditingChanged];
    [_password addTarget:self action:@selector(changePassword:) forControlEvents:UIControlEventEditingChanged];
    [_surePass addTarget:self action:@selector(changeSurePass:) forControlEvents:UIControlEventEditingChanged];
}
- (void)changePhone:(UITextField *)sender
{
    if (sender.text.length > 0)
    {
        _modifyModel.oldPassword = sender.text;
    }
}
- (void)changePassword:(UITextField *)sender
{
    if (sender.text.length > 0)
    {
        _modifyModel.password = sender.text;
    }
}
- (void)changeSurePass:(UITextField *)sender
{
    if (sender.text.length > 0)
    {
        _modifyModel.surePass = sender.text;
    }
}
- (IBAction)commitClick:(UIButton *)sender
{
    if (_modifyModel.oldPassword.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"旧密码不能为空!"];
         return;
    }
    if (_modifyModel.password.length < 6)
    {
        [SVProgressHUD showInfoWithStatus:@"密码长度至少6位!"];
        return;

    }
    if (![_modifyModel.surePass isEqualToString:_modifyModel.password])
    {
        [SVProgressHUD showInfoWithStatus:@"两次密码不一致!"];
        return;
    }
    [_dataDic setObject:[CountHeight sha1:[NSString stringWithFormat:@"%@%@",get_sp(@"app_user"),_modifyModel.oldPassword]] forKey:@"old_pass"];
    [_dataDic setObject:[CountHeight sha1:[NSString stringWithFormat:@"%@%@",get_sp(@"app_user"),_modifyModel.password]] forKey:@"new_pass"];
    [MDataProvider modifyPassword:_dataDic sucess:^(NSDictionary *resultDic)
     {
         [SVProgressHUD showSuccessWithStatus:resultDic[@"msg"]];
         remove_sp(@"app_user");
         remove_sp(@"app_pass");
         remove_sp(@"company_id");
         remove_sp(@"company_name");
         remove_sp(@"company");
         
    } faile:^(NSString *str)
    {
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
@end
